What is read-pkg?
The read-pkg npm package is used to read a package.json file and parse its contents. It can normalize the data according to the npm normalization rules, handle different file encodings, and can read from different locations by resolving the path to the nearest package.json file.
What are read-pkg's main functionalities?
Read package.json from the current directory
This feature reads the package.json file from the current working directory and outputs the parsed JSON object.
const readPkg = require('read-pkg');
(async () => {
const pkg = await readPkg();
console.log(pkg);
})();
Read package.json from a specified directory
This feature allows you to specify a directory from which to read the package.json file, rather than the current working directory.
const readPkg = require('read-pkg');
(async () => {
const pkg = await readPkg({ cwd: 'path/to/directory' });
console.log(pkg);
})();
Read and normalize package.json data
This feature reads the package.json file and normalizes its data according to npm normalization rules, which can be useful for ensuring consistent data structure.
const readPkg = require('read-pkg');
(async () => {
const pkg = await readPkg({ normalize: true });
console.log(pkg);
})();
Other packages similar to read-pkg
read-package-json
read-package-json is an npm package that reads the package.json file and also provides normalization. It is the library that npm itself uses to read package.json files, which means it's well-tested and reliable, but it might be more complex and tightly coupled with npm's internal use cases compared to read-pkg.
load-json-file
load-json-file is a package that provides a simple API for reading and parsing JSON files. It does not specifically target package.json files, so it lacks the normalization features of read-pkg, but it can be used for general JSON file reading purposes.
pkg-conf
pkg-conf is a package that reads configuration from package.json files. It's focused on retrieving configuration settings rather than reading the entire package.json file, so it serves a slightly different purpose compared to read-pkg.
read-pkg
Read a package.json file
Why
Install
npm install read-pkg
Usage
import {readPackage} from 'read-pkg';
console.log(await readPackage());
console.log(await readPackage({cwd: 'some-other-directory'}));
API
readPackage(options?)
Returns a Promise<object>
with the parsed JSON.
readPackageSync(options?)
Returns the parsed JSON.
options
Type: object
cwd
Type: URL | string
Default: process.cwd()
Current working directory.
normalize
Type: boolean
Default: true
Normalize the package data.
parsePackage(packageFile, options?)
Parses an object or string into JSON.
Note: packageFile
is cloned using structuredClone
to prevent modification to the input object. This function is available from Node.js 18 on. In environments without structuredClone
(such as Node.js 16), a shallow spread is used instead, which can cause deep properties of the object to be modified. Consider cloning the object before using parsePackage
if that's the case.
packageFile
Type: object | string
An object or a stringified object to be parsed as a package.json.
options
Type: object
normalize
Type: boolean
Default: true
Normalize the package data.
Related